/**
 * 定义站点地图模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.bcSerial.bcSerialManager', ['water.entity.bcSerial']);
		
	module.controller('bcSerialManagerController',function($scope,$bcSerialService,$state){

		/**
		 * 设备列表数据
		 */
		$scope.list;
		
		/**
		 * 是否显示列表
		 */
		$scope.isShowList = true;
		/**
		 * 是否显示新建页面
		 */
		$scope.isShowAdd = false;
		/**
		 * 是否显示编辑页面
		 */
		$scope.isShowEdit = false;
		
		/**
		 * 用于新建的model
		 */
		$scope.addModel;
		/**
		 * 用于编辑的model
		 */
		$scope.editModel;
		
		/**
		 * 串口协议集合
		 */
		$scope.protocols = [];
		
		/**
		 * 奇偶校验
		 */
		$scope.parity = [
		                 {k:'0',v:'NONE'},
		                 {k:'1',v:'ODD'},
		                 {k:'2',v:'EVEN'},
		                 {k:'3',v:'MARK'},
		                 {k:'4',v:'SPACE'}
		                 ];
		
		/**
		 * 波特率
		 */
		$scope.baudRates = [
		                 {k:'110',v:'110'},
		                 {k:'300',v:'300'},
		                 {k:'600',v:'600'},
		                 {k:'1200',v:'1200'},
		                 {k:'2400',v:'2400'},
		                 {k:'4800',v:'4800'},
		                 {k:'9600',v:'9600'}
		                 ];
		
		/**
		 * 串口号
		 */
		$scope.comNums = [
						{k:'COM1',v:'COM1'},
						{k:'COM2',v:'COM2'},
						{k:'COM3',v:'COM3'},
						{k:'COM4',v:'COM4'},
						{k:'COM5',v:'COM5'},
						{k:'COM6',v:'COM6'},
						{k:'COM7',v:'COM7'},
						{k:'COM8',v:'COM8'},
						{k:'COM9',v:'COM9'},
						{k:'COM10',v:'COM10'},
						{k:'COM11',v:'COM11'},
						{k:'COM12',v:'COM12'},
            			{k:'COM13',v:'COM13'},
            			{k:'COM14',v:'COM14'},
            			{k:'COM15',v:'COM15'},
            			{k:'COM16',v:'COM16'},
						{k:'NONE',v:'NONE'}
		                 ];
		
		/**
		 * 停止位
		 */
		$scope.stopBits = [
						{k:'1',v:'1'},
						{k:'1.5',v:'1.5'},
						{k:'2',v:'2'}
		                 ];
		
		/**
		 * 停止位
		 */
		$scope.stopBits = [
						{k:'1',v:'1'},
						{k:'1.5',v:'1.5'},
						{k:'2',v:'2'}
		                 ];
		/**
		 * 数据位
		 */
		$scope.dataBits = [
						{k:'5',v:'5'},
						{k:'6',v:'6'},
						{k:'7',v:'7'},
						{k:'8',v:'8'}
		                 ];
		
		/**
		 * 打开新增窗口按钮点击事件
		 */
		$scope.onOpenCreateClick = function(){
			$scope.addModel = {};
			$scope.showAdd();
		}
		
		/**
		 * 保存按钮点击事件
		 */
		$scope.onSaveClick = function(model){
			$bcSerialService.create(model).then(function(){
				$scope.getList();
				$scope.showList();
				alert("保存成功!");
				$scope._init();
			}).catch(function(error){
				alert(error);
			});
		}
		
		/**
		 * 打开编辑窗口按钮点击事件
		 */
		$scope.onOpenEditClick = function(model){
			$scope.editModel = model;
			$scope.showEdit();
		}
		
		/**
		 * 更新按钮点击事件
		 */
		$scope.onUpdateClick = function(model){
			$bcSerialService.update(model).then(function(){
				$scope.getList();
				$scope.showList();
				alert("保存成功!");
				$scope._init();
			}).catch(function(error){
				alert(error);
			});
		}
		
		/**
		 * 删除按钮点击事件
		 */
		$scope.onDeleteClick = function(id){
			$bcSerialService.deleteModel(id).then(function(){
				$scope.getList();
				alert("删除成功!");
				$scope._init();
			});
		}
		
		/**
		 * 取消按钮点击事件
		 */
		$scope.onCancelClick = function(){
			$scope.showList();
		}
		
		/**
		 * 只显示列表
		 */
		$scope.showList = function(){
			$scope.isShowList = true;
			$scope.isShowAdd = false;
			$scope.isShowEdit = false;
		}
		
		/**
		 * 只显示新建页
		 */
		$scope.showAdd = function(){
			$scope.isShowList = false;
			$scope.isShowAdd = true;
			$scope.isShowEdit = false;
		}
		
		/**
		 * 只显示编辑页
		 */
		$scope.showEdit = function(){
			$scope.isShowList = false;
			$scope.isShowAdd = false;
			$scope.isShowEdit = true;
		}
		
		$scope.getList = function(){
			$bcSerialService.getList().then(function(data){
				$scope.list = data;
			});
		}
		
		/**
		 * 获取串口协议列表
		 */
		$scope.getProtocolList = function(){
			$bcSerialService.getProtocolList().then(function(data){
				for(var i=0;i<data.length;i++){
					var item = data[i];
					$scope.protocols.push({
						k:item.id,
						v:item.name
					});
				}
			});
			$scope.protocols = [];
		}
		
		$scope._init = function(){
			$scope.getList();
			$scope.showList();
			$scope.getProtocolList();
		}
		
		$scope._init();
	});
	
}(window, angular));


